package cn.parllay.dingdangproject.widget;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.parllay.dingdangproject.R;
import cn.parllay.dingdangproject.bean.notice.Notice;
import cn.parllay.dingdangproject.utils.SpUtils;

/**
 * Created by sunny on 2017/9/4.
 */

public class MyWindowManager1 {

    private CenterWindow centerWindow = null;
    private BottomWindow bottomWindow = null;
    private static MyWindowManager1 manager = null;
    private static Object lock = new Object();
    private Context mContext;

    private MyWindowManager1() {

    }

    public static MyWindowManager1 getInstance() {
        if (manager == null) {
            synchronized (lock) {
                if (manager == null) {
                    manager = new MyWindowManager1();
                }
            }
        }
        return manager;
    }

    public void init(Context context) {

        mContext = context;

        if (centerWindow == null) {
            centerWindow = new CenterWindow(context);
        }
        if (bottomWindow == null) {
            bottomWindow = new BottomWindow(context);
        }
    }

    public CenterWindow getCenterWindow() {
        return centerWindow;
    }

    public BottomWindow getBottomWindow() {
        return bottomWindow;
    }

    /**
     * 弹出中间提醒弹窗
     */
    public void popupCenterWindow(Notice notice) {
        centerWindow.popupCenterWindow(notice);
    }

    /**
     * 弹出底部提醒弹窗
     */
    public void popupBottomWindow(List<Notice> notices) {
        bottomWindow.popupBottomWindow(notices);
    }

    public void disMissWindow() {

        disMissCenterWindow();
        disMissBottomWindow();

    }

    private void disMissCenterWindow() {
        centerWindow.dismiss();
        centerWindow = null;
    }

    private void disMissBottomWindow() {
        bottomWindow.dismiss();
        bottomWindow = null;
    }


    public void popNoticeWindow(Notice notice) {

        Log.e("TAG", "弹出弹窗");
        //播放提示音并振动
        dingDangAndShake();

        if (!centerWindow.isShow() && !bottomWindow.isShow()) {//如果都没弹出就弹中间的
            Log.e("TAG", "弹出中间弹窗");
            popupCenterWindow(notice);
        } else {//否则弹底部的
            Log.e("TAG", "弹出底部弹窗");
            List<Notice> notices = new ArrayList<>();
            notices.add(notice);
            popupBottomWindow(notices);
        }
    }

    private void dingDangAndShake() {
        AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
        int currVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC) ;// 当前的媒体音量
        final int ringerMode = am.getRingerMode();
        switch (ringerMode) {
            case AudioManager.RINGER_MODE_NORMAL:
                playDingDang(R.raw.notice,currVolume);
                shake(1000);
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                shake(1000);
                break;
            case AudioManager.RINGER_MODE_SILENT:

                break;
        }
    }

    private void playDingDang(int ring, int curVome) {
        if (SpUtils.getInstance(mContext).getBoolean(SpUtils.IS_RING_ON, true)) {
            MediaPlayer mediaPlayer = MediaPlayer.create(mContext, ring);
            mediaPlayer.setVolume(curVome,curVome);
            mediaPlayer.start();
        }
    }

    private void shake(long shakeTime) {
        if (SpUtils.getInstance(mContext).getBoolean(SpUtils.IS_SHAKE_ON, true)) {
            Vibrator vibrator = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE);
            vibrator.vibrate(shakeTime);
        }
    }
}
